﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CH.Easy.Utility;
using CH.Easy.UI.Domain.PageAgg;
using CH.Easy.DbProvider.Mapping;
using CH.Easy.UI.Common;
using CH.Easy.Ioc;

namespace CH.Easy.UI.Domain.ModuleAgg
{


    [TableMapping(Name = "ModuleConfig", PrimaryKey = "ID", IsIdentity = true)]
    public class BaseModule :Entity
    {

        public int ID { get; set; }
        /// <summary>
        /// 模块所属组件
        /// </summary>
        public int ComponentID { get; set; }
        /// <summary>
        /// 模块模板
        /// </summary>
        public string Template { get; set; }
        /// <summary>
        /// 模块模板路径
        /// </summary>
        public string TemplatePath { get; set; }
        /// <summary>
        /// 模块名字
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// IocKey
        /// </summary>
        public string IocKey { get; set; }
        /// <summary>
        /// 是否为公共模块
        /// </summary>
        public bool IsPublic { get; set; }
        /// <summary>
        /// 是否是使用内置模板
        /// </summary>
        public bool IsUseInsideTemplate { get; set; }
        /// <summary>
        /// id=2|name=xx|pid=qs(pid)|cid=qs(cid)
        /// </summary>
        public string ParamValues { get; set; }
        /// <summary>
        /// Headers 私有js css
        /// </summary>
        public string Headers { get; set; }















        public List<string> HeaderList = new List<string>();
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        protected BasePage page { get; set; }
        public BaseModule()
        {

        }
        public BaseModule Create(BasePage page) 
        {

            BaseModule am = IocContainer.Resolve<BaseModule>(this.IocKey);
            am.ID = this.ID;
            am.Headers = this.Headers;
            am.IocKey = this.IocKey;
            am.IsPublic = this.IsPublic;
            am.IsUseInsideTemplate = this.IsUseInsideTemplate;
            am.ParamValues = this.ParamValues;
            am.Template = this.Template;
            am.TemplatePath = this.TemplatePath;
            am.Title = this.Title;
            am.page = page;
            return am;
        }
        public virtual void PreInit()
        {
            this.TemplatePath = this.page.SiteConfig.FullSkinPath + this.TemplatePath;
        }
        public virtual void Init() { }
        public virtual void SetDataSource() { }
        public virtual StringBuilder GetTemplate()
        {
            StringBuilder Template = new StringBuilder();
            if (this.IsUseInsideTemplate)
            {
                return Template.Append(this.Template);
            }
            else
            {
                return Template.Append(FileHelper.GetFileByString(this.TemplatePath));
            }
        }
        protected virtual void SetData(string key, object value)
        {
            if (this.page.Data.ContainsKey(key))
            {
                throw new ArgumentException("存在重复的key:" + key);
            }

            this.page.Data.Add(key, value);

        }

    }
}
